package org.kjtc.service;

import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.kjtc.bean.Constant;
import org.kjtc.entity.WxChargingstationEntity;
import org.kjtc.entity.WxCollectionEntity;
import org.kjtc.mapper.WxChargingstationMapper;
import org.kjtc.redis.RedisUtil;
import org.kjtc.weChatUtil.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.*;


import java.util.*;

/**
 * @Author: xiaojinlu1990@ 163.com
 * @Date: 2019/3/21 0021.
 * @Version 1.0
 * 获取站桩的详情信息
 */
@CacheConfig(cacheNames="wxt"/*,cacheManager = "employeeCacheManager"*/) //抽取缓存的公共配置
@Service
public class MapFindingPileService {
    private static Logger logger = Logger.getLogger(MapFindingPileService.class);

    @Autowired
    private Constant constant;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WxChargingstationMapper wxChargingstationMapper;


    public List<Map<String,Object>> collectionLately(String longitude, String latitude,String openid) {
        if(StringUtils.isEmpty(openid)){
            openid = "testUserOpenid";
        }

        List<WxChargingstationEntity> list = wxChargingstationMapper.selectList(openid);
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashedMap();
            WxChargingstationEntity wxEntity =  list.get(i);
            //获取桩ID
            String chargingstationid = wxEntity.getChargingstationid();
            map.put("latitude",wxEntity.getLatitude());//
            map.put("longitude",wxEntity.getLongitude());//
            map.put("chargingstationid",wxEntity.getChargingstationid());//
            map.put("Enabled",wxEntity.getEnabled());//
            //获取所有的附近的站信息
            double calcultion = StringUtils.getDistance(latitude,longitude,""+wxEntity.getLatitude(),""+wxEntity.getLongitude());
            String distanceValue =null;
            logger.info("constant:rangeValue:"+redisUtil.get("constant:rangeValue"));
            if (redisUtil.get("constant:rangeValue")!=null)
                  distanceValue = (String) redisUtil.get("constant:rangeValue");
                else
                distanceValue = constant.getRangeValue();
            if(null != distanceValue) {
                if (NumberUtils.stringToDouble(distanceValue) > calcultion) {
                    map.put("distanceM", (int) calcultion);//距离米
                    map.put("chargingstationname", wxEntity.getChargingstationname());//名称
                    map.put("chargingstationaddress", wxEntity.getChargingstationaddress());//地址
                    map.put("chargingstationshortname", wxEntity.getChargingstationshortname());//短名称
                }
            }
            putChagingStationMasseges(map,wxEntity.getChargingstationid());

            listMap.add(map);
        }

        return  listMap;
    }



    /**  获取在101.201.42.248
     * 是否在线  桩数
     * 获取
     * @param map
     * @param chargingstationid
     */
    private void putChagingStationMasseges(Map<String, Object> map, String chargingstationid) {
        String jsonReslut = getTastTimeMessage(chargingstationid);
        Map<String, Object> chastation = JsonUtils.parseJsonToMap(jsonReslut);
        String stutas = (String) chastation.get("result");
        List timeCharg = null;
        Map<String, Object> chastationTaskTime = null;
        if ("success".equals(stutas)){
            timeCharg  = (List) chastation.get("chargingModel");
            chastationTaskTime = (Map<String, Object>) chastation.get("chargingStationData");
        }
        //计算尖峰平谷电价
        String electricityPrice ="2.1";
        //获取当前时间

        int newTime = DateUtil.formatDateHHForInt(new Date());
        if(null != timeCharg)
        for (int i = 0; i < timeCharg.size(); i++) {
            Map<String,Object> mapmodel = (Map<String, Object>) timeCharg.get(i);
            String startDTTM = (String) mapmodel.get("startDTTM");
            String endDTTM = (String) mapmodel.get("endDTTM");
            if(Integer.parseInt(startDTTM.replace(":","")) <=newTime && newTime<= Integer.parseInt(endDTTM.replace(":","")) )
            {
                Double number =(Double) mapmodel.get("electricPrice");
                electricityPrice = number+"";

            }

        }
        map.put("onlineStatus",chastationTaskTime!=null?chastationTaskTime.get("onlineStatus"):"false"); //在线状态   true  false
        map.put("electricityPrice",electricityPrice);//电价 11.2
        map.put("serviceCharge",chastationTaskTime!=null?chastationTaskTime.get("serviceCharge"):"1");//服务费4.1
        map.put("enableCount",chastationTaskTime!=null?chastationTaskTime.get("enableCount"):"0");//在线数 8
        map.put("disableCount",chastationTaskTime!=null?chastationTaskTime.get("disableCount"):"0");//离线数 1
        map.put("allnumbers",chastationTaskTime!=null?chastationTaskTime.get("allnumbers"):"0");//总数数


    }
   // @Cacheable(value = {"wxt"})
    public String getTastTimeMessage(String chargingstationid) {
        String url = "http://www.kejiatc.cn/billOperate/getChargingStationMassage";
        Map<String, String> params = new HashMap<>();
        params.put("chargingStationID",chargingstationid);
        Map<String, String> heads = new HashMap<>();
        return HttpClientUtil.httpGet(url,params,heads);
    }

    public List<Map<String,Object>> collectionestHigth() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        for (int i = 0; i < 10; i++) {
            Map<String,Object> map = new HashedMap();
            map.put("aa","测试一");
            map.put("bb","测试二");
            map.put("cc","测试三");
            list.add(map);
        }
        return  list;
    }

    public List<Map<String,Object>> collectionMessage() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashedMap();
        map.put("aa","测试一");
        map.put("bb","测试二");
        map.put("cc","测试三");
        list.add(map);
        return  list;
    }

    /**
     * 获取所以站信息
     * @return
     */

    public List<WxChargingstationEntity> chargingstationList() {

       return wxChargingstationMapper.selectList("");
    }

    public Map mpCollectionStationMassage( String chargingStationID, String openid,String type) {
        if(StringUtils.isEmpty(openid)){
            openid ="defalt";
        }
        Map<String,String> map = new HashedMap();
        map.put("chargingStationID",chargingStationID);
        WxCollectionEntity wxCollection = new WxCollectionEntity();
        wxCollection.setOpenid(openid);
        wxCollection.setChargingStationId(chargingStationID);
        wxCollection.setEnabled(1);
        wxCollection.setCreateDttm(DateUtil.getTimeShortString(new Date(),"yyyy-MM-dd HH:mm:ss"));
        wxCollection.setCreateUser(openid);
        wxCollection.setUpdateDttm(DateUtil.getTimeShortString(new Date(),"yyyy-MM-dd HH:mm:ss") );
        wxCollection.setUpdateUser(openid);
        //收藏
        if ("1".equals(type)){
            try{
                WxCollectionEntity collection = wxChargingstationMapper.selectCollection(wxCollection);
                if(collection != null) {
                    wxCollection.setEnabled(1);
                    //取消收藏

                        int wxCollectionEntity = wxChargingstationMapper.updateCollection(wxCollection);
                        if (wxCollectionEntity != 0) {
                            map.put("message", "success1");
                        }
                    }else{
                        int wxCollectionEntity = wxChargingstationMapper.insertCollection(wxCollection);
                        if (wxCollectionEntity != 0) {
                            map.put("message", "success1");
                        }
                    }

            }catch (Exception e){
                e.printStackTrace();
                map.put("message","fail");
            }
        }else{
            wxCollection.setEnabled(0);
            //取消收藏
            try{
                int wxCollectionEntity = wxChargingstationMapper.updateCollection(wxCollection);
                if (wxCollectionEntity != 0 ){
                    map.put("message","success0");
                }
            }catch (Exception e){
                e.printStackTrace();
                map.put("message","fail");
            }
        }




        return map;
    }

}
